package com.ymatou.iapi.fastturnover.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fastturnover.parameter.FastTurnoverWithdrawApplyReq;
import com.ymatou.iapi.fastturnover.parameter.bean.AccountInfo;
import com.ymatou.iapi.fastturnover.parameter.bean.WithdrawApplyDetailVo;
import com.ymatou.iapi.fastturnover.service.FastTurnoverWithdrawApplyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.database.model.biinputdb.SellerLevelHistory;
import com.ymttest.database.model.fastturnover.TurnoverApply;
import com.ymttest.database.model.fastturnover.TurnoverApplyDetail;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.database.sqlwapper.billinputdb.SellerLevelHistoryWrapper;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyDetailWrapper;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 取消订单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_FastTurnoverWithdrawApply {

	private static FastTurnoverWithdrawApplyReq fastTurnoverWithdrawApplyReq;
	private static FastTurnoverWithdrawApplyCall fastTurnoverWithdrawApplyCall;
	
	private static TurnoverApplyWrapper turnoverApplyWrapper = new TurnoverApplyWrapper();
	private static TurnoverApplyDetailWrapper turnoverApplyDetailWrapper = new TurnoverApplyDetailWrapper();
	
	private static SellerLevelHistoryWrapper sellerLevelHistoryWrapper = new SellerLevelHistoryWrapper();
	
	private static AccountWapper accountWapper = new AccountWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建提现申请单");
	}

	@Before
	public void caseUp() {
		fastTurnoverWithdrawApplyReq = new FastTurnoverWithdrawApplyReq();
		fastTurnoverWithdrawApplyCall = new FastTurnoverWithdrawApplyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(fastTurnoverWithdrawApplyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyTurnoverApply(TurnoverApply expectTurnoverApply,
			TurnoverApply actualTurnoverApply) {

		Logger.comment("验证turnover_apply：" + actualTurnoverApply.getApply_no());

		DataUtil.verifyDecimalEquals(
				expectTurnoverApply.getApply_total_amount(),
				actualTurnoverApply.getApply_total_amount(),
				"验证Apply_total_amount");
		DataUtil.verifyEquals(expectTurnoverApply.getWithdraw_currency(),
				actualTurnoverApply.getWithdraw_currency(),
				"验证Withdraw_currency");
		DataUtil.verifyEquals(expectTurnoverApply.getSeller_id(),
				actualTurnoverApply.getSeller_id(), "验证Seller_id");
		DataUtil.verifyEquals(expectTurnoverApply.getPay_status(),
				actualTurnoverApply.getPay_status(), "验证Pay_status");
		DataUtil.verifyEquals(expectTurnoverApply.getRepay_status(),
				actualTurnoverApply.getRepay_status(), "验证Repay_status");
	}
	
	public void verifyTurnoverApplyDetail(
			TurnoverApplyDetail expectTurnoverApplyDetail,
			TurnoverApplyDetail actualTurnoverApplyDetail) {

		Logger.comment("验证turnover_apply_detail：" + actualTurnoverApplyDetail.getApply_no());

		DataUtil.verifyDecimalEquals(
				expectTurnoverApplyDetail.getSeller_id(),
				actualTurnoverApplyDetail.getSeller_id(),
				"验证Seller_id");
		DataUtil.verifyEquals(expectTurnoverApplyDetail.getApply_amount(),
				actualTurnoverApplyDetail.getApply_amount(),
				"验证Apply_amount");
		DataUtil.verifyEquals(expectTurnoverApplyDetail.getService_amount(),
				actualTurnoverApplyDetail.getService_amount(), "验证Service_amount");
		DataUtil.verifyEquals(expectTurnoverApplyDetail.getCommission_amount(),
				actualTurnoverApplyDetail.getCommission_amount(), "验证Commission_amount");
		DataUtil.verifyEquals(expectTurnoverApplyDetail.getSettlement_currency(),
				actualTurnoverApplyDetail.getSettlement_currency(), "验证Settlement_currency");
		DataUtil.verifyEquals(expectTurnoverApplyDetail.getStatus(),
				actualTurnoverApplyDetail.getStatus(), "验证Status");
	}
	
	public void verify() throws InterruptedException {

		TurnoverApply actualTurnoverApply = turnoverApplyWrapper
				.selectBySellerIdAndPayStatus(
						fastTurnoverWithdrawApplyReq.getSellerId(), 0);
		
		Long sellerId = fastTurnoverWithdrawApplyReq.getSellerId();
		
		TurnoverApply expectTurnoverApply = new TurnoverApply();
		
		expectTurnoverApply.setApply_total_amount(fastTurnoverWithdrawApplyReq
				.getWithdrawApplyDetailVos().get(0).getApplyAmount());
		expectTurnoverApply.setWithdraw_currency(fastTurnoverWithdrawApplyReq
				.getWithdrawCurrency());
		expectTurnoverApply.setSeller_id(sellerId);
		expectTurnoverApply.setPay_status(0);
		expectTurnoverApply.setRepay_status(0);
		
		this.verifyTurnoverApply(expectTurnoverApply, actualTurnoverApply);
		
		String applyNo = actualTurnoverApply.getApply_no();
		
		TurnoverApplyDetail actualTurnoverApplyDetail = turnoverApplyDetailWrapper
				.selectBySellerIdAndApplyNo(
						sellerId, applyNo);
		
		TurnoverApplyDetail expectTurnoverApplyDetail = new TurnoverApplyDetail();
		
		for (WithdrawApplyDetailVo withdrawApplyDetailVo : fastTurnoverWithdrawApplyReq.getWithdrawApplyDetailVos()) {
			
			BigDecimal commissionAmount = BigDecimal.ZERO;
			BigDecimal applyAmount = withdrawApplyDetailVo.getApplyAmount();
			
			if (withdrawApplyDetailVo.getSettlementCurrency() == 1) {
				
				commissionAmount = new BigDecimal("5.00");
				
			} else {
				
				commissionAmount = new BigDecimal("35.00").add(applyAmount
						.multiply(new BigDecimal("0.003")).setScale(2,
								BigDecimal.ROUND_HALF_DOWN));
			}
			
			expectTurnoverApplyDetail.setApply_no(applyNo);
			expectTurnoverApplyDetail.setSeller_id(sellerId);
			expectTurnoverApplyDetail.setApply_amount(applyAmount);
			
			BigDecimal serviceAmount = applyAmount.multiply(
					new BigDecimal("0.005")).setScale(2,
					BigDecimal.ROUND_HALF_DOWN);
			
			expectTurnoverApplyDetail
					.setService_amount(serviceAmount.compareTo(new BigDecimal(
							"100.00")) == -1 ? new BigDecimal("100.00")
							: serviceAmount);
			expectTurnoverApplyDetail.setCommission_amount(commissionAmount);
			expectTurnoverApplyDetail.setSettlement_currency(withdrawApplyDetailVo.getSettlementCurrency());
			expectTurnoverApplyDetail.setStatus(1);
			
			this.verifyTurnoverApplyDetail(expectTurnoverApplyDetail, actualTurnoverApplyDetail);
			
			turnoverApplyDetailWrapper.updateToFinishByApplyNo(applyNo);
		}
		
		turnoverApplyWrapper.updateToFinishByApplyNo(applyNo);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FastTurnoverWithdrawApply_001() {
		Logger.start(true, "创建提现申请，外币");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			Long sellerId = 20227567L;
			
			SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
			
			sellerLevelHistory.setDDate(YMTDateUtil.getDate());
			sellerLevelHistory.setSellerID(sellerId.intValue());
			sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
			sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
			
			sellerLevelHistoryWrapper.insertSelective(sellerLevelHistory);
			
			List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
			
			WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
			AccountInfo accountInfo = new AccountInfo();
			
			// 获取重置前的可用金额
			BigDecimal availAmount = accountWapper
					.selectAccountInfoByUserId(sellerId.intValue(), 1).get(0)
					.getAvailAmount();
			
			String accountid1 = AccountCallServiceV2.getAccountId(sellerId.intValue(), 1);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					"20000"), BigDecimal.ZERO);
			
			// 买手提现银行账号
			accountInfo.setAccountNo("dd369e93-5198-45c7-b4d5-fc27c702606a");
			accountInfo.setType(1);
			
			withdrawApplyDetailVo.setAccountInfo(accountInfo);
			withdrawApplyDetailVo.setApplyAmount(new BigDecimal("10001.00"));
			withdrawApplyDetailVo.setSettlementCurrency(4);
			
			withdrawApplyDetailVos.add(withdrawApplyDetailVo);
			
			fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
			fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
			fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

			// 发送请求
			fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
			fastTurnoverWithdrawApplyCall.callService();
			
			Logger.verifyEquals(200, fastTurnoverWithdrawApplyCall.getRetultCode(),
					"验证Code");
			
			AccountCallServiceV2.resetAccount(accountid1, availAmount, BigDecimal.ZERO);

//			// 数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FastTurnoverWithdrawApply_002() {
		Logger.start(true, "申请金额小于10000");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
			
			WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
			AccountInfo accountInfo = new AccountInfo();
			Long sellerId = 24635724L;
			
			accountInfo.setAccountNo("autotest" + TestDataManager.getRandomIntNum(10));
			accountInfo.setType(1);
			
			withdrawApplyDetailVo.setAccountInfo(accountInfo);
			withdrawApplyDetailVo.setApplyAmount(new BigDecimal("9999.00"));
			withdrawApplyDetailVo.setSettlementCurrency(4);
			
			withdrawApplyDetailVos.add(withdrawApplyDetailVo);
			
			fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
			fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
			fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

			// 发送请求
			fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
			fastTurnoverWithdrawApplyCall.callService();
			
			Logger.verifyEquals(202, fastTurnoverWithdrawApplyCall.getRetultCode(),
					"验证Code");
			Logger.verifyEquals("单次申请不能低于10000元", fastTurnoverWithdrawApplyCall.getMsg(),
					"验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FastTurnoverWithdrawApply_003() {
		Logger.start(true, "申请提现金额超过可申请金额");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
			
			WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
			AccountInfo accountInfo = new AccountInfo();
			Long sellerId = 24635724L;
			
			accountInfo.setAccountNo("autotest" + TestDataManager.getRandomIntNum(10));
			accountInfo.setType(1);
			
			withdrawApplyDetailVo.setAccountInfo(accountInfo);
			withdrawApplyDetailVo.setApplyAmount(new BigDecimal("9999.00"));
			withdrawApplyDetailVo.setSettlementCurrency(4);
			
			withdrawApplyDetailVos.add(withdrawApplyDetailVo);
			
			fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
			fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
			fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

			// 发送请求
			fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
			fastTurnoverWithdrawApplyCall.callService();
			
			Logger.verifyEquals(202, fastTurnoverWithdrawApplyCall.getRetultCode(),
					"验证Code");
			Logger.verifyEquals("申请提现金额超过可申请金额", fastTurnoverWithdrawApplyCall.getMsg(),
					"验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FastTurnoverWithdrawApply_004() {
		Logger.start(true, "最多只能有一笔申请中的快周转提现单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
			
			WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
			AccountInfo accountInfo = new AccountInfo();
			Long sellerId = 3383L;
			
			String accountid1 = AccountCallServiceV2.getAccountId(sellerId.intValue(), 1);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					"20000"), BigDecimal.ZERO);
			
			accountInfo.setAccountNo("autotest" + TestDataManager.getRandomIntNum(10));
			accountInfo.setType(1);
			
			withdrawApplyDetailVo.setAccountInfo(accountInfo);
			withdrawApplyDetailVo.setApplyAmount(new BigDecimal("10001.00"));
			withdrawApplyDetailVo.setSettlementCurrency(4);
			
			withdrawApplyDetailVos.add(withdrawApplyDetailVo);
			
			fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
			fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
			fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

			// 发送请求
			fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
			fastTurnoverWithdrawApplyCall.callService();
			
			Logger.verifyEquals(202, fastTurnoverWithdrawApplyCall.getRetultCode(),
					"验证Code");
			Logger.verifyEquals("最多只能有一笔申请中的快周转提现单", fastTurnoverWithdrawApplyCall.getMsg(),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FastTurnoverWithdrawApply_005() {
		Logger.start(true, "创建提现申请，人民币");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			Long sellerId = 20227567L;
			
			SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
			
			sellerLevelHistory.setDDate(YMTDateUtil.getDate());
			sellerLevelHistory.setSellerID(sellerId.intValue());
			sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
			sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
			
			sellerLevelHistoryWrapper.insertSelective(sellerLevelHistory);
			
			List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
			
			WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
			AccountInfo accountInfo = new AccountInfo();
			
			// 获取重置前的可用金额
			BigDecimal availAmount = accountWapper
					.selectAccountInfoByUserId(sellerId.intValue(), 1).get(0)
					.getAvailAmount();
			
			String accountid1 = AccountCallServiceV2.getAccountId(sellerId.intValue(), 1);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					"200000"), BigDecimal.ZERO);
			
			// 买手提现银行账号
			accountInfo.setAccountNo("dd369e93-5198-45c7-b4d5-fc27c702606a");
			accountInfo.setType(1);
			
			withdrawApplyDetailVo.setAccountInfo(accountInfo);
			withdrawApplyDetailVo.setApplyAmount(new BigDecimal("15001.00"));
			withdrawApplyDetailVo.setSettlementCurrency(1);
			
			withdrawApplyDetailVos.add(withdrawApplyDetailVo);
			
			fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
			fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
			fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

			// 发送请求
			fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
			fastTurnoverWithdrawApplyCall.callService();
			
			Logger.verifyEquals(200, fastTurnoverWithdrawApplyCall.getRetultCode(),
					"验证Code");
			
			AccountCallServiceV2.resetAccount(accountid1, availAmount, BigDecimal.ZERO);

//			// 数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}

